#include "interpreter.h"
#include<unistd.h>
#include<time.h>

void runner(const string& fname,int u_id);
void test();

int main(int argc,char* argv[]){
    //待读取文件相对路径
    string relativePath = "in1.txt";
    if(argc == 2){
        relativePath = argv[1];
        // cout << "相对路径:" << relativePath << endl;
    }

    //获取当前路径
    char path[200];
	if(!getcwd(path,200)){
		cout<<"Get path fail!"<<endl;
		return 0;
	}
    string absolutePath(path);
    // cout << "当前路径" << absolutePath << endl;

    //合成路径
    for(int i=1; i<relativePath.length(); i++){
        absolutePath += relativePath[i];
    }
    // cout << "合成路径:" << absolutePath << endl;

    //根据读入in文件名字生成out文件名 比如 in1.txt => out1.txt
    string outFileAbsPath = absolutePath;
    outFileAbsPath.replace(outFileAbsPath.find("YIN"),3,"YTEST");
    // cout << "输出路径:" << outFileAbsPath << endl;
    //清空输出文件
    fstream clear_file;
    clear_file.open(outFileAbsPath,ios::out | ios::trunc);
    clear_file.close();
    string fname;
    int uid;
    //打开测试文件
    fstream file;
    file.open(absolutePath,ios::in);
    if(!file.is_open()){
        cout << "open file error" << endl;
        exit(-1);
    }
    //脚本文件名
    file >> fname >> uid;
    int sleep_times,sleep_length;
    //睡眠周期数 周期时间
    file >> sleep_times >> sleep_length;
    //用户两次输入 第二次可能为空
    string user_input1,user_input2;
    file >> user_input1 >> user_input2;

    file.close();
    // cout << "脚本文件:" << fname << endl
    //     << "查询用户:" << uid << endl
    //     << "睡眠次数:" << sleep_times << endl
    //     << "睡眠时间:" << sleep_length << endl
    //     << "用户输入1:" << user_input1 << endl
    //     << "用户输入2:" << user_input2 << endl;
    
    // return 0;
    Interpreter I(fname);
    I.getUserInfo(uid);
    //测试属性通过公有属性传递
    I.sleep_times = sleep_times;
    I.sleep_length = sleep_length;
    I.v_user_input.push_back(user_input1);
    I.v_user_input.push_back(user_input2);
    I.outFilePath = outFileAbsPath;
    I.parseFile();
    I.InterpreterRunner(true);
    return 0;
}

void runner(const string& fname,int u_id)
{
    // Interpreter I(fname);
    // I.getUserInfo(u_id);
    // I.parseFile();
    // I.InterpreterRunner();
}
void test()
{

}